//
//  HMCircleAnimator.m
//  02-转场动画
//
//  Created by Apple on 17/1/11.
//  Copyright © 2017年 Apple. All rights reserved.
//

#import "HMCircleAnimator.h"

@interface HMCircleAnimator ()<UIViewControllerAnimatedTransitioning,CAAnimationDelegate>
@property (nonatomic, weak) id <UIViewControllerContextTransitioning>transitionContext;
//  是否是展示控制器
@property (nonatomic) BOOL isPresented;
@end
@implementation HMCircleAnimator
#pragma mark UIViewControllerTransitioningDelegate
/// 弹出控制器时调用   返回值:由谁来弹出控制器

- (nullable id <UIViewControllerAnimatedTransitioning>)animationControllerForPresentedController:(UIViewController *)presented presentingController:(UIViewController *)presenting sourceController:(UIViewController *)source
{
    _isPresented = YES;// 弹出控制器
    return self;
}

/// 销毁控制器时调用  返回值:由谁来销毁控制器
- (nullable id <UIViewControllerAnimatedTransitioning>)animationControllerForDismissedController:(UIViewController *)dismissed
{
    _isPresented = NO;// 销毁控制器
    return self;
}

#pragma mark UIViewControllerAnimatedTransitioning
/// 动画的持续时间
//  transitionContext:转场上下文
- (NSTimeInterval)transitionDuration:(nullable id <UIViewControllerContextTransitioning>)transitionContext
{
    
    return 4.0;
}

/// 动画转场方法
//  transitionContext:转场上下文
- (void)animateTransition:(id <UIViewControllerContextTransitioning>)transitionContext
{
    self.transitionContext = transitionContext;
    // 1.获取容器视图
    UIView *containerView = [transitionContext containerView];
    
    /// 2.获取目标视图   对应的是ViewController
    UIView *toView = [transitionContext viewForKey:UITransitionContextToViewKey];
    
    // 3.获取来源视图   对应的是黄色的控制器
    UIView *fromView = [transitionContext viewForKey:UITransitionContextFromViewKey];
//    NSLog(@"toView %@      fromView %@",toView,fromView);
    
    UIView *view = _isPresented ? toView : fromView;
//    NSLog(@"%@",view);
    // 4.展现目标视图
    if (_isPresented) {
        [containerView addSubview:toView];
    }
    
    // 5.添加动画
    [self circleViewView:view];
    
}

- (void)circleViewView:(UIView *)view
{
        // 创建图层
    CAShapeLayer *shapeLayer = [CAShapeLayer layer];
    // 获取路径
    CGFloat radius = 30; // 直径
    CGFloat margin = 28; // 上边间距
    CGFloat rightMargin = 8;
    CGFloat viewW = view.bounds.size.width; //屏幕宽度
    CGFloat viewH = view.bounds.size.height;//屏幕高度
    CGRect rect = CGRectMake(viewW - rightMargin - radius, margin, radius, radius);


    UIBezierPath *startPath = [UIBezierPath bezierPathWithOvalInRect:rect];
//    //    // 给图层赋值路径
    shapeLayer.path = startPath.CGPath;
//    //
//    // 填充颜色 -->当添加遮罩后  填充颜色不会显示
//    //    shapeLayer.fillColor = [UIColor redColor].CGColor;
//
//
//    // 获取屏幕对角线的距离
    CGFloat maxRadius = sqrt(viewW * viewW + viewH * viewH);
    CGRect endRect = CGRectInset(rect, -maxRadius, -maxRadius);
    UIBezierPath *bezierPath = [UIBezierPath bezierPathWithOvalInRect:endRect];
    // 给图层赋值路径
    shapeLayer.path = bezierPath.CGPath;
//
//    // 添加遮罩
    view.layer.mask = shapeLayer;
//
//    // 添加动画
    CABasicAnimation *basicAnimation = [CABasicAnimation animationWithKeyPath:@"path"];
    basicAnimation.duration = 0.25;// 动画的时间
    if (_isPresented) { // 弹出控制器
        // 动画开始的位置
        basicAnimation.fromValue = (__bridge id _Nullable)(startPath.CGPath);
        
        // 动画结束的位置
        basicAnimation.toValue = (__bridge id _Nullable)(bezierPath.CGPath);
    }else{// 销毁控制器
        // 动画开始的位置
        basicAnimation.fromValue = (__bridge id _Nullable)(bezierPath.CGPath);
        
        // 动画结束的位置
        basicAnimation.toValue = (__bridge id _Nullable)(startPath.CGPath);
    }
    
    basicAnimation.fillMode = kCAFillModeForwards;// 模式
    basicAnimation.removedOnCompletion = NO;// 完成后删除
    // 设置代理   不能放在动画添加到图层方法之后
    basicAnimation.delegate = self;
    // 添加动画到图层上
    [shapeLayer addAnimation:basicAnimation forKey:nil];

}

/// 动画完成后调用
- (void)animationDidStop:(CAAnimation *)anim finished:(BOOL)flag
{
    // 6.结束转场  -->不结束转场  目标视图不能交互   如果动画没有执行完  就结束转场  会关闭动画
    [self.transitionContext completeTransition:YES];
}
@end
